<template>
  <div class="permission-container">
    <!-- headerTool -->
    <headTool style="margin:20px">
      <template #after>
        <el-button type="primary" @click="showAdd('0', 1)">添加权限</el-button>
      </template>
    </headTool>
    <!-- table -->
    <el-card style="margin:20px">
      <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-link type="primary" @click="showAdd(row.id, 2)">添加</el-link>
            <el-link
              type="primary"
              style="margin:0 20px"
              @click="doEdit(row.id)"
              >编辑</el-link
            >
            <el-link type="primary" @click="doDel(row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="title" :visible="showDialog" @close="cancelDialog">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="权限名称">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item prop="code" label="权限标识">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button @click="save" size="mini" type="primary">确定</el-button>
          <el-button size="mini" @click="cancelDialog">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPermissionList,
  getPermissionDetail,
  addPermission,
  updatePermission,
  removePermission
} from '@/api/permission'
// 树形结构递归
import { toTreeData } from '@/utils'
export default {
  name: 'permission',

  data () {
    return {
      tableData: [],
      showDialog: false,
      formData: {},
      rules: {}
    }
  },
  created () {
    this.getPermissionList()
  },
  computed: {
    title () {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList () {
      const res = await getPermissionList()
      console.log('所有权限列表', res.data)
      // 返回的数据是列表数据，要转换成树形数据
      this.tableData = toTreeData(res.data, '0')
    },
    // 添加点击事件
    showAdd (pid, type) {
      // 如果是一级 pid为'0',type为1
      // 如果是二级 pid为当前这行权限的id,type为2
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 对话框确定的点击事件
    async save () {
      if (this.formData.id) {
        // 有id，证明是编辑
        // 调用编辑请求
        await updatePermission(this.formData)
      } else {
        // 调用接口发请求
        await addPermission(this.formData)
        console.log('formData', this.formData)
      }

      // 弹出提示
      this.$message.success(this.title + '成功！')
      // 刷新列表
      this.getPermissionList()
      // 隐藏对话框
      this.showDialog = false
    },
    // 给取消按钮添加点击事件
    cancelDialog () {
      // 清空表单数据
      this.formData = {}
      // 重置表单校验规则
      this.$refs.form.resetFields()
      // 隐藏对话框
      this.showDialog = false
    },
    // 编辑点击事件
    async doEdit (id) {
      const res = await getPermissionDetail(id)
      this.formData = res.data
      this.showDialog = true
    },
    // 删除事件
    async doDel (id) {
      // 发请求
      await removePermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    }
  }
}
</script>

<style></style>
